package appcus

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/app"
	"counter-help/admin/server/model/app/request"
	"counter-help/admin/server/model/app/response"
	"counter-help/admin/server/model/customer"
	"errors"
)

type CusOrderEvaluateService struct{}

func (e *CusOrderEvaluateService) CreateCusOrderEvaluate(userID int, evaInfo *request.OrderEvaReq) (err error) {
	// 检查订单状态是否为 6（已完成）
	var order customer.CusOrder
	err = global.GVA_DB.Model(&customer.CusOrder{}).Where("id = ? AND status = 6", evaInfo.OrderId).First(&order).Error
	if err != nil {
		return errors.New("订单未完成，无法进行评价")
	}

	// 检查用户是否为最终接单用户
	var assignment customer.CusOrderAssignments
	err = global.GVA_DB.Model(&customer.CusOrderAssignments{}).Where("user_id = ? AND order_id = ? AND is_final = 1", userID, evaInfo.OrderId).First(&assignment).Error
	if err != nil {
		return errors.New("不是最终接单用户, 无权进行评价")
	}

	// 构造评价信息
	createEvaInfo := app.OrderEvaluate{
		Content: evaInfo.Content,
		UserId:  userID,
		OrderId: evaInfo.OrderId,
	}

	// 创建评价
	if err = global.GVA_DB.Model(&app.OrderEvaluate{}).Create(&createEvaInfo).Error; err != nil {
		return err
	}
	return nil
}

// DeleteCusOrderEvaluate 删除评价
func (e *CusOrderEvaluateService) DeleteCusOrderEvaluate(userID int, evaluateInfo *request.DeleteOrderEvaReq) (err error) {
	// 检查评价是否属于用户
	var evaluate app.OrderEvaluate
	err = global.GVA_DB.Model(&app.OrderEvaluate{}).Where("id = ? AND user_id = ?  AND order_id = ?", evaluateInfo.EvaluateId, userID, evaluateInfo.OrderId).First(&evaluate).Error
	if err != nil {
		return errors.New("评价不存在或无权删除")
	}

	// 删除评价
	if err = global.GVA_DB.Delete(&evaluate).Error; err != nil {
		return err
	}
	return nil
}

// UpdateCusOrderEvaluate 修改评价
func (e *CusOrderEvaluateService) UpdateCusOrderEvaluate(userID int, evaluateInfo *request.UpdateOrderEvaReq) (err error) {
	// 检查评价是否属于用户
	var evaluate app.OrderEvaluate
	err = global.GVA_DB.Model(&app.OrderEvaluate{}).Where("id = ? AND user_id = ? AND order_id = ?", evaluateInfo.EvaluateId, userID, evaluateInfo.OrderId).First(&evaluate).Error
	if err != nil {
		return errors.New("评价不存在或无权修改")
	}

	// 更新评价内容
	evaluate.Content = evaluateInfo.Content
	if err = global.GVA_DB.Model(&app.OrderEvaluate{}).Save(&evaluate).Error; err != nil {
		return err
	}
	return nil
}

// GetOrderEvaluateByOrderID 根据订单ID查看所有评价
func (e *CusOrderEvaluateService) GetOrderEvaluateByOrderID(orderID int) (evaluates []response.GetOrderEvaRes, err error) {
	// 查询该订单的所有评价记录
	var evaOrderRecs []app.OrderEvaluate
	err = global.GVA_DB.Model(&app.OrderEvaluate{}).Where("order_id = ?", orderID).Find(&evaOrderRecs).Error
	if err != nil {
		return nil, errors.New("查询评价记录失败: " + err.Error())
	}

	// 创建返回结果的切片
	evaluates = make([]response.GetOrderEvaRes, 0, len(evaOrderRecs))

	// 遍历评价记录，根据 user_id 查询用户信息
	for _, eval := range evaOrderRecs {
		var user customer.CusUser
		err = global.GVA_DB.Model(&customer.CusUser{}).
			Where("id = ?", eval.UserId).
			First(&user).Error
		if err != nil {
			return nil, errors.New("查询用户信息失败: " + err.Error())
		}

		// 组装 GetOrderEvaRes 结构并添加到结果列表中
		evaluates = append(evaluates, response.GetOrderEvaRes{
			UserId:   eval.UserId,
			Avatar:   user.Avatar,
			NickName: user.Nickname,
			Content:  eval.Content,
		})
	}

	return evaluates, nil
}

// GetUnionAllOrderEvaluate 根据订单ID查看所有评价 联表查询
func (e *CusOrderEvaluateService) GetUnionAllOrderEvaluate() (evaluates []response.GetAllOrderEvaRes, err error) {
	// 使用联表查询一次性获取评价记录以及对应的用户信息
	err = global.GVA_DB.Table("cus_order_evaluate AS oe").
		Select("oe.user_id, oe.order_id, u.avatar, u.nickname, oe.evaluate").
		Joins("INNER JOIN cus_user AS u ON u.id = oe.user_id").
		Find(&evaluates).Error
	if err != nil {
		return nil, errors.New("查询评价记录失败: " + err.Error())
	}

	return evaluates, nil
}
